package com.youlu.campus.web.config;

import cn.hippo4j.core.executor.DynamicThreadPool;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : [dengbo]
 * @className : AsyncThreadConfig
 * @description : [描述说明该类的功能]
 * @createTime : [2022/7/14 18:49]
 */
@Configuration
@Slf4j
public class AsyncThreadConfig implements AsyncConfigurer {

    @Bean
    @DynamicThreadPool
    public Executor myThreadPoolTaskExecutor() {
        String threadPoolId = "async-dynamic";
        ThreadPoolExecutor executor = ThreadPoolBuilder.builder()
                .threadFactory(threadPoolId)
                .threadPoolId(threadPoolId)
                .dynamicPool()
                .build();
        return executor;
    }

    @Override
    public Executor getAsyncExecutor() {
        return myThreadPoolTaskExecutor();
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (throwable, method, objects) -> {
            log.error("getAsyncUncaughtExceptionHandler,method:{},e", method, throwable);
        };
    }
}
